package com.xindz.cn.mvptest.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

/**
 * @name MvpTest
 * @class name：com.xindz.cn.mvptest.base
 * @describe describe
 * @anthor XINDZ
 * @time 2017/10/27 9:27
 * @change
 * @chang time
 * @class describe
 */
public abstract class BaseFragment<V extends BaseView, P extends BasePresenter<V>> extends Fragment {
    public boolean NotMVP = false;
    private P presneter;
    private V view;

    public P getPresneter() {
        return presneter;
    }

    public void setNotMVP() {
        NotMVP = true;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!NotMVP) initMVP();
    }

    private void initMVP() {
        if (this.presneter == null) {
            //创建P层
            this.presneter = createPresneter();
        }

        if (this.view == null) {
            //创建V层
            this.view = createView();
        }
        //判定是否为空
        if (this.presneter == null) {
            throw new NullPointerException("presneter不能够为空");
        }
        if (this.view == null) {
            throw new NullPointerException("view不能够为空");
        }
        //绑定
        this.presneter.attachView(this.view);
    }

    //并不知道具体的P是哪一个实现类，由他的子类决定(BaseActivity子类决定具体类型)
    public abstract P createPresneter();

    //并不知道具体的V是哪一个实现类，由他的子类决定(BaseActivity子类决定具体类型)
    public abstract V createView();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (this.presneter != null)
            this.presneter.detachView();
    }
}
